<template>
  <div class="">
    <el-card class="box-card">
      <div class="b-right">
        <el-button type="primary" @click="addRoleListBox">添加角色</el-button>
      </div>
      <template>
        <el-table :data="rolesTableData" stripe border style="width: 100%">
          <el-table-column width="80" type="expand">
            <!-- 下拉 -->
            <template slot-scope="scope">

              <el-row v-for="item in scope.row.children" :key="item.id">
                <el-col :span="5">
                  <el-tag closable @close="removeRightById(scope.row,item.id)" type="warning">{{item.authName}}</el-tag>
                  <i class="el-icon-caret-right"></i>
                </el-col>
                <!-- 二级 -->
                <el-col :span="19">
                  <el-row v-for="item2 in item.children" :key="item2.id">
                    <el-col :span="5">
                      <el-tag closable @close="removeRightById(scope.row,item2.id)" type="warning">{{item2.authName}}
                      </el-tag>
                      <i class="el-icon-caret-right"></i>
                    </el-col>
                    <!-- 三级 -->
                    <el-col :span="18">
                      <el-tag :closable=closable @close="removeRightById(scope.row,item3.id)" type="warning"
                        v-for="(item3) in item2.children" :key="item3.id">
                        {{item3.authName}}
                      </el-tag>
                    </el-col>
                  </el-row>
                </el-col>


              </el-row>

            </template>
            <!-- 下拉 -->
          </el-table-column>
          <el-table-column prop="id" label="id" width="180">
          </el-table-column>
          <el-table-column prop="roleName" label="角色名称" width="180">
          </el-table-column>
          <el-table-column prop="roleDesc" label="角色描述" width="180">
          </el-table-column>
          <el-table-column prop="address" label="角色操作">
            <template slot-scope="scope">
              <!-- 修改 -->

              <el-button type="primary" icon="el-icon-edit" size='mini' @click="editRolesDialog(scope.row)">编辑
              </el-button>

              <!-- 删除 -->

              <el-button type="danger" @click="deletRoles(scope.row.id)" icon="el-icon-delete" size='mini'>
                删除
              </el-button>

              <!-- 分配角色 -->

              <el-button type="warning" @click="allotRoles(scope.row)" icon="el-icon-setting" size='mini'>
                分配权限
              </el-button>

            </template>
          </el-table-column>
        </el-table>
      </template>
    </el-card>
    <!-- 新增角色弹窗 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <el-form :model="addRolesData" status-icon ref="addRolesDataForm" label-width="100px" class="demo-ruleForm">

        <el-form-item label="角色名称">
          <el-input v-model="addRolesData.roleName" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="角色描述">
          <el-input v-model="addRolesData.roleDesc" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addRoleList">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑提交角色弹窗 -->
    <el-dialog title="提示" :visible.sync="editDialogVisible" width="30%">
      <el-form :model="editRolesData" status-icon ref="editRolesDataForm" label-width="100px" class="demo-ruleForm">

        <el-form-item label="角色id">
          <el-input v-model="editRolesData.id" autocomplete="off" disabled></el-input>
        </el-form-item>

        <el-form-item label="角色名称">
          <el-input v-model="editRolesData.roleName" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="角色描述">
          <el-input v-model="editRolesData.roleDesc" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editRoleList">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 分配权限弹窗 -->
    <el-dialog title="分配权限" :visible.sync="allotDialogVisible" width="30%">

      <el-tree show-checkbox default-expand-all :default-checked-keys="selectedKeyArr" ref="allotRolesData"
        :data="allotRolesData" node-key='id' :props="defaultProps" @check="handleCheckChange">
      </el-tree>

      <span slot="footer" class="dialog-footer">
        <el-button @click="allotDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitSetRights">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
  export default {
    components: {},
    data() {
      return {
        rolesTableData: [],
        closable: false,
        // 新增弹窗变量
        dialogVisible: false,
        // 修改弹窗变量
        editDialogVisible: false,
        // 新增提交
        addRolesData: {
          id: '',
          roleName: '',
          roleDesc: ''
        },
        // 修改提交
        editRolesData: {
          roleName: '',
          roleDesc: ''
        },
        // 分配权限
        allotRolesData: [],

        allotDialogVisible: false,

        defaultProps: {
          children: 'children',
          label: 'authName'
        },
        // 勾选id数组
        selectedKeyArr: [],
        // 角色id
        roleId: ''
      }
    },
    created() {
      this.initRolesData()
    },
    mounted() {},
    watch: {},
    computed: {},
    methods: {
      initRolesData() {
        this.$axios.get('roles').then(val => {
          this.rolesTableData = val.data
        })
      },
      //   添加角色打开弹窗
      addRoleListBox() {
        this.dialogVisible = true
      },
      // 添加角色
      addRoleList() {
        this.$axios.post('roles', this.addRolesData).then(val => {
          if (val.meta.status != 201) {
            return this.$message.error(val.meta.msg)
          }
          this.initRolesData()
          this.$message.success('创建成功！')
        })
        this.dialogVisible = false
        this.$refs.addRolesDataForm.resetFields()
      },
      // 删除角色
      deletRoles(id) {

        this.$axios.delete('roles/' + id).then(val => {
          this.initRolesData()
        })
      },
      // 打开修改角色弹窗
      editRolesDialog(val) {
        this.editRolesData.id = val.id
        this.editRolesData.roleName = val.roleName
        this.editRolesData.roleDesc = val.roleDesc
        this.editDialogVisible = true

      },
      // 修改角色
      editRoleList() {
        this.$axios.put('roles/' + this.editRolesData.id, {
          roleName: this.editRolesData.roleName,
          roleDesc: this.editRolesData.roleDesc
        }).then(val => {
          if (val.meta.status != 200) {
            return this.$message.error(val.meta.msg)
          }
          this.initRolesData()
          this.$message.success('编辑成功！')
          this.editDialogVisible = false
        })
      },
      // 删除权限
      removeRightById(val, id) {
        this.$axios.delete('roles/' + val.id + '/rights/' + id).then(val => {
          if (val.meta.status != 200) {
            return this.$message.error(val.meta.msg)
          }
          this.initRolesData()
          this.$message.success('删除权限成功！')

        })
      },

      // 分配权限弹窗
      allotRoles(role) {
        this.selectedKeyArr = []
        this.roleId = role.id
        this.allotDialogVisible = true
        this.$axios.get('rights/tree').then(val => {

          this.allotRolesData = val.data

        })
        this.getLeafKeys(role, this.selectedKeyArr)

      },
      //该函数会获取到当前角色的所有三级权限id并添加到defKeys中
      getLeafKeys(node, arr) {
        //如果当前节点不包含children属性，则表示node为三级权限
        if (!node.children) {
          return arr.push(node.id)
        }
        //递归调用
        node.children.forEach(item => this.getLeafKeys(item, arr))
      },
      // 勾选事件
      handleCheckChange() {



      },
      // 提交设置权限按钮
      submitSetRights() {
        this.allotDialogVisible = false
        this.selectedKeyArr = [
          ...this.$refs.allotRolesData.getHalfCheckedKeys(),
          ...this.$refs.allotRolesData.getCheckedKeys()
        ]
        let str = this.selectedKeyArr.join(',')
        this.$axios.post('roles/' + this.roleId + '/rights', {
          rids: str
        }).then(val => {

          if (val.meta.status != 200) {
            return this.$message.error(val.meta.msg)
          }
          this.$message.success(val.meta.msg)
          this.initRolesData()
        })
      }

    },
  }

</script>
<style lang="css" scoped>
  .b-right {
    float: left;
    margin-bottom: 20px;
  }

  .el-tag {
    margin: 5px;
  }

</style>
